package com.flea.hclv.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.flea.hclv.model.dao.HProductAgencyOrderMapper;
import com.flea.hclv.model.dao.HProductMapper;
import com.flea.hclv.model.dao.HTimeTableMapper;
import com.flea.hclv.model.entity.HProduct;
import com.flea.hclv.model.entity.HProductAgencyOrder;
import com.flea.hclv.model.entity.HTimeTable;
import com.flea.hclv.service.HProductAgencyOrderService;
import com.flea.hclv.service.HTimeTableService;
import com.flea.hclv.util.ServerResponse;
import com.flea.system.base.util.StringUtil;
import com.flea.system.bean.FleaPage;
import com.flea.system.platform.session.FleaOrgUtils;
import com.flea.system.utils.FleaStringUtils;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class HProductAgencyOrderServiceImpl implements HProductAgencyOrderService {
  @Autowired
  private HProductAgencyOrderMapper hProductAgencyOrderMapper;
  @Autowired
  private HTimeTableService hTimeTableService;
  @Autowired
  private HProductMapper productMapper;

  @Autowired
  private HTimeTableMapper hTimeTableMapper;

  @Override
  public ServerResponse addProduceAgency(String model, HttpServletRequest request)
      throws Exception {
    if(StringUtil.isEmpty(model)){
      return ServerResponse.createByErrorMessage("参数为空异常，请联系管理员");
    }
    HProductAgencyOrder jsb = JSONObject.parseObject(model,HProductAgencyOrder.class);
    if(jsb==null){
      return ServerResponse.createByErrorMessage("参数转版异常，JSON数据为空");
    }
    List<String> visitorIds = jsb.getVisitorIds();
    if(null==visitorIds||visitorIds.size()==0){
      return ServerResponse.createByErrorMessage("请选择游客信息，游客信息为空");
    }
    String currentUserId = FleaOrgUtils.getCurUserUuid(request);
    String currentUserDeptUuid = FleaOrgUtils.getCurrentUserDeptUuid(request);
    List<HProductAgencyOrder> addList = new ArrayList<>();
    jsb.setCreateUser(currentUserId);
    jsb.setProvideDeptId(currentUserDeptUuid);
    jsb.setValid("Y");
    jsb.setModifyUser(currentUserId);
    String timeTableId = jsb.getTimeTableId();
    for (String visitorId: visitorIds
    ) {
      HProductAgencyOrder temp = new HProductAgencyOrder();
      temp.setId(FleaStringUtils.getGenerateUUID());
      temp.setValid(jsb.getValid());
      temp.setStatus(jsb.getStatus());
      temp.setProvideDeptId(jsb.getProvideDeptId());
      temp.setAgencyedDept(jsb.getAgencyedDept());
      temp.setProuductId(jsb.getProuductId());
      temp.setTimeTableId(jsb.getTimeTableId());
      temp.setTotalCost(jsb.getTotalCost());
      temp.setTotalSale(jsb.getTotalSale());
      temp.setVisitorId(visitorId);
      temp.setCreateUser(jsb.getCreateUser());
      temp.setModifyUser(jsb.getModifyUser());

      addList.add(temp);
    }
    int personNum = visitorIds.size();
    //更新product的timeTableId
    hProductAgencyOrderMapper.insertBatch(addList);

   boolean result = hTimeTableService.updateTimeTableRestSub(timeTableId,personNum,currentUserId);
   if(result){
     throw new Exception("班期人数更新失败，请检查原因，并联系运维人员");
   }

    return ServerResponse.createBySuccessMessage("代客下单操作成功！");
  }

  @Override
  public ServerResponse updateAgencyById(String model, HttpServletRequest request)
      throws Exception {
    if(StringUtil.isEmpty(model)){
      return ServerResponse.createByErrorMessage("参数为空异常，请联系管理员");
    }
    HProductAgencyOrder jsb = JSONObject.parseObject(model,HProductAgencyOrder.class);
    if(jsb==null){
      return ServerResponse.createByErrorMessage("参数转版异常，JSON数据为空");
    }
    String currentUserId = FleaOrgUtils.getCurUserUuid(request);
    String currentUserDeptUuid = FleaOrgUtils.getCurrentUserDeptUuid(request);
    jsb.setModifyUser(currentUserId);
    boolean resullt = hProductAgencyOrderMapper.updateByPrimaryKeySelective(jsb)>0;
   if(resullt){
     return ServerResponse.createBySuccessMessage("更新信息成功");
   }else{
     return ServerResponse.createByErrorMessage("信息更新失败");
   }
  }

  /**
   * 纯粹的软删除
   * @param model
   * @param request
   * @return
   * @throws Exception
   */
  @Override
  public ServerResponse deleteAgencyByIds(String model, HttpServletRequest request)
      throws Exception {

    if(StringUtil.isEmpty(model)){
      return ServerResponse.createByErrorMessage("参数为空，非法，请联系运维人员");
    }
    List<String> ids = JSONObject.parseArray(model,String.class);
    if(null==ids || ids.size()==0){
      return ServerResponse.createByErrorMessage("参数转版异常，非法，请联系运维人员");
    }

    String userId = FleaOrgUtils.getCurUserUuid(request);
    hProductAgencyOrderMapper.deleteBatchInvalid(ids,userId);



   return ServerResponse.createBySuccessMessage("删除成功！");
  }

  @Override
  public ServerResponse<FleaPage<HProductAgencyOrder>> find4Paging(HttpServletRequest request,
      int curPage, int pageSize) {
    String curDeptUuid = FleaOrgUtils.getCurrentUserDeptUuid(request);
    RowBounds rowBounds = new RowBounds(curPage,pageSize);
    List<HProductAgencyOrder> result = hProductAgencyOrderMapper.find4Paging(rowBounds,curDeptUuid);
    FleaPage<HProductAgencyOrder> pageData = new FleaPage<>(result);
    List<HProductAgencyOrder> newResult = this.coverCodeToName(result);
    pageData.setCurPageData(newResult);
    return ServerResponse.createBySuccess(pageData);
  }

  private List<HProductAgencyOrder> coverCodeToName(List<HProductAgencyOrder> datas){
    List<HProductAgencyOrder> newList = new ArrayList<>();
    for (HProductAgencyOrder agency:datas) {
      String productId = agency.getProuductId();
      String timeTableId = agency.getTimeTableId();
      HProduct p =  productMapper.selectByPrimaryKey(productId);
      HTimeTable hTimeTable = hTimeTableMapper.selectByPrimaryKey(timeTableId);
      String creatUseName =  FleaOrgUtils.getUserByUserUuid(agency.getCreateUser()).getUserName();
      String suplierName = FleaOrgUtils.getOrgDeptByDeptUuid(agency.getProvideDeptId()).getDeptName();
      agency.setProduct(p);
      agency.sethTimeTable(hTimeTable);
      agency.setCreatUserName(creatUseName);
      agency.setSuplierName(suplierName);
      newList.add(agency);
    }



    return newList;
  }

}
